/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vs.kindlecoll.Util;

import com.vs.kindlecoll.IO.IOConfig;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author volodymyr.sadovyy
 */
public class HashGen {

	static private MessageDigest mess;
    static private String DOCS_PATH_PREFIX = "/mnt/us/documents/";
    static private String HASH_ALGORITHM = "SHA1";

    static public boolean init() {
        try {
            mess = MessageDigest.getInstance(HASH_ALGORITHM);
            return true;
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(HashGen.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }

    }

    static public String getHashString(File f) throws UnsupportedEncodingException {
        if (null == f || !f.exists()) {
            return "";
        }

        //Special Kindle Hash codes handling
        if (f.getName().contains(IOConfig.ASIN)){
        	return getASINExceptionalHash(f.getName());
        } 

        StringBuilder sb = new StringBuilder(DOCS_PATH_PREFIX);

        String path;

        path = f.getPath();
        if (null != path) {
            int b;
            b = path.indexOf(IOConfig.DOCUMENTS_DIR_NAME) + IOConfig.DOCUMENTS_DIR_NAME.length() + File.separator.length();

            sb.append(path.substring(b, path.length()).replaceAll("\\\\", "/"));
        }


        return getHashString(sb.toString());
    }

    private static String getASINInMiddExceptionalHash(String name) {
		return name.substring(name.indexOf(IOConfig.ASIN) + IOConfig.ASIN_LENGTH)
				.replace(IOConfig.TYPE_SUFFIX, IOConfig.TYPE_SUFFIX_REPLACEMENT)
				.replace(IOConfig.EBOK, IOConfig.EBOK_REPLACEMENT)
				.replace(IOConfig.VERSION_SUFFIX, IOConfig.VERSION_SUFFIX_REPLACEMENT)
				.replace(IOConfig.AZW_FILE_EXT, IOConfig.AZW_FILE_EXT_REPLACEMENT);
	}

	private static String getASINExceptionalHash(String name) {
		return name.substring(name.indexOf(IOConfig.ASIN))
				.replace(IOConfig.ASIN, IOConfig.ASIN_REPLACEMENT)
				.replace(IOConfig.TYPE_SUFFIX, IOConfig.TYPE_SUFFIX_REPLACEMENT)
				.replace(IOConfig.PDOC, IOConfig.PDOC_REPLACEMENT)
				.replace(IOConfig.EBOK, IOConfig.EBOK_REPLACEMENT)
				.replace(IOConfig.VERSION_SUFFIX, IOConfig.VERSION_SUFFIX_REPLACEMENT)
				.replace(IOConfig.AZW_FILE_EXT, IOConfig.AZW_FILE_EXT_REPLACEMENT);
	}

	static private String getHash(String s) throws UnsupportedEncodingException {
        return getHash(s.getBytes(IOConfig.JSON_ENCODING));
    }
    
    static private String getHashString(String s) throws UnsupportedEncodingException{
        return "*" + getHash(s);
    }

    static private String getHash(byte[] bytes) {
        if (null == mess) {
            init();
        }
        mess.reset();
        return toHexView(mess.digest(bytes));
    }

    static private String toHexView(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
        }
        return sb.toString();
    }
}
